package genetic;

import genetic.treenodes.Addition;
import genetic.treenodes.Constant;
import genetic.treenodes.Division;
import genetic.treenodes.Multiplication;
import genetic.treenodes.Subtraction;
import genetic.treevisualizer.METreeBranchRenderer;
import genetic.treevisualizer.METreeNodeRenderer;
import genetic.treevisualizer.TreeVisualizer;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class T03_serializeGP {

	public static void main(String[] args) {
		ArrayList<GPTreeNode> functionSet = new ArrayList<GPTreeNode>();
		ArrayList<GPTreeNode> terminalSet = new ArrayList<GPTreeNode>();
		
		functionSet.add(new Addition());
		functionSet.add(new Subtraction());
		functionSet.add(new Multiplication());
		functionSet.add(new Division());
		
		terminalSet.add(new Constant(11.11));
		terminalSet.add(new Constant(22.22));
		
		GeneticProgram gp = new GeneticProgram(functionSet, terminalSet);
		gp.createRandom(3, 1);

		System.out.println(gp.eval());
		
		TreeVisualizer tv = new TreeVisualizer();
		tv.setTreeBranchRenderer(new METreeBranchRenderer());
		tv.setTreeNodeRenderer(new METreeNodeRenderer());
		tv.setBranchStartWidth(10.0);
		tv.setArenaColor(Color.black);
		tv.setSide(1024);
		tv.setCircleDiminishFactor(0.75);
		tv.writeImageFile(tv.renderTree(gp.getRoot()), new File("ser_write.png"));		
/*
		try {
			FileOutputStream fs = new FileOutputStream("obj.ser");
			ObjectOutputStream os = new ObjectOutputStream(fs);

			os.writeObject(gp);
			os.close();
		} catch (IOException e) {
			System.err.println(e.toString());
		}
*/
		GeneticProgram gp2 = null;
		
		try {
			FileInputStream fs = new FileInputStream("obj.ser");
			ObjectInputStream is = new ObjectInputStream(fs);
			gp2 = (GeneticProgram) is.readObject();
			is.close();
		} catch (ClassNotFoundException e) {
			System.err.println(e.toString());
		} catch (IOException e) {
			System.err.println(e.toString());
		}
		System.out.println(gp2.eval());
		
		tv.writeImageFile(tv.renderTree(gp2.getRoot()), new File("ser_read.png"));		
	
	}

}
